import React, { PureComponent, Fragment } from 'react';
import { connect } from 'dva';
import router from 'umi/router';
import {Icon} from 'antd';
import PageHeaderLayout from '../../layouts/PageHeaderLayout';
import styles from '../Pbulic.less';


const tabList = [
  {
    key: "info",
    tab: "备件详情",
  },
  {
    key: "related",
    tab: "关联设备",
  },
  {
    key: "userecord",
    tab: "使用记录",
  },
  {
    key: "operationlog",
    tab: "操作日志",
  }
];


@connect()
export default class EquipmentDetail extends PureComponent {
  constructor(props) {
    super(props);
    this.state={
      tabDefaultKeys:"info",
    }
  }


  componentDidMount() {

  }

  componentWillUnmount(){
    const { dispatch } = this.props;
    dispatch({
      type: `equipment_spares/appendClearState`,
    })
  }


  // 路由切换
  handleTabChange = key => {
    const { dispatch, location, match } = this.props;
    const detailInfoId = match.params.id;
    router.push({ pathname:`/equipment/spares/${detailInfoId}/${key}`,  query: { ...location.query}});
  };

  formatterMenu = (data) => {
    return data.reduce((arr, curr) => {
      if(curr.authority){
        if (checkPermissions(curr.authority)) {
          return arr.concat(curr);
        } else {
          return arr.concat([]);
        }
      }else{
        return arr.concat(curr);
      }
    }, []);

  };

  goBack = () => {
    // this.props.onClose(0);
    const { dispatch, location } = this.props;
    router.push({ pathname:'/equipment/spares/list',  query: { ...location.query} });
  }

  render() {
    const { match, history, routerData, dictionaries, children} = this.props;

    const location_arry = history.location.pathname.split("/");
    const tabDefaultKeys = location_arry[location_arry.length - 1];
    const detailInfoId = match.params.id && match.params.id!='detail' ? match.params.id : 0;


    // const tabDefaultTitle = [{title:tabList.find(item=>item.key==tabDefaultKeys).tab}];

    return (
      <Fragment>
        <Icon type="close-circle-o"  className={styles.close_icon} onClick={this.goBack}/>
        <PageHeaderLayout
          tabList={this.formatterMenu(tabList)}
          tabActiveKey={tabDefaultKeys}
          onTabChange={this.handleTabChange}
          hiddenBreadcurmb={true}
          title={"备件详情"}
        >
          {children}

        </PageHeaderLayout>
      </Fragment>
    );
  }
}
